package com.lc.activiti.sys.controller;


import cn.hutool.json.JSONObject;
import com.lc.activiti.config.Result;
import com.lc.activiti.sys.entity.SysUser;
import com.lc.activiti.sys.service.SysUserService;
import com.lc.activiti.utils.JwtUtil;
import com.lc.activiti.utils.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;

/**
 * 人员表(SysUser)表控制层
 *
 * @author makejava
 * @since 2021-05-20 10:52:46
 */
@RestController
@RequestMapping("sysUser")
@Api(tags = "人员")
public class SysUserController{
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;


    /**
     * 保存用户信息
     * @author 骆超
     * @date 2021/5/20
     * @param sysUser 用户对象
     * @return com.lc.activiti.config.Result
     */
    @PostMapping(value = "saveUserInfo",name = "保存用户信息")
    @ApiOperation("保存用户信息")
    public Result saveUserInfo(@RequestBody SysUser sysUser){
        return Result.success(sysUserService.saveUserInfo(sysUser));
    }

    /**
     * 用户登录
     * @author 骆超
     * @date 2021/5/21
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping(value = "loginAction",name = "用户登录")
    @ApiOperation("用户登录")
    public Result loginAction(@RequestBody SysUser sysUser){
        return sysUserService.loginAction(sysUser);
    }

    @PostMapping(value = "logOutAction",name = "用户退出")
    @ApiOperation("用户退出")
    public Result logOutAction(){
        return sysUserService.logOutAction();
    }

    /**
     * 获取用户列表
     * @author 骆超
     * @date 2021/5/21
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping(value = "getUserList",name = "获取用户列表")
    @ApiOperation("获取用户列表")
    public Result getUserList(@RequestBody SysUser sysUser){
        return Result.success(sysUserService.getUserList(sysUser));
    }

    /**
     * 获取当前登录用户信息
     * @author 骆超
     * @date 2021/7/5
     * @return com.lc.activiti.config.Result
     */
    @PostMapping(value = "getUserInfo",name = "获取当前登录用户信息")
    @ApiOperation("获取当前登录用户信息")
    public Result getUserInfo(){
        return Result.success(JwtUtil.getUser());
    }



    /**
     * 批量保存组织用户
     * @author 骆超
     * @date 2021/7/5
     * @return com.lc.activiti.config.Result
     */
    @PostMapping(value = "saveDeptUsers",name = "批量保存组织用户")
    @ApiOperation("批量保存组织用户")
    public Result saveDeptUsers(@RequestBody SysUser sysUser){
        return sysUserService.saveDeptUsers(sysUser);
    }

    @PostMapping(value = "sengMsg")
    public void sengMsg(@RequestBody JSONObject jsonObject) throws IOException {
        WebSocketServer.sendByUserIds(jsonObject.getStr("msg"), Arrays.asList(jsonObject.getStr("userIds").split(",")));
    }

    /**
     * 发送注册短信验证码
     * @author 骆超
     * @date 2021/7/13
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("sendRegisterCode")
    @ApiOperation("发送注册短信验证码")
    public Result sendRegisterCode(@RequestBody SysUser sysUser){
        return sysUserService.sendRegisterCode(sysUser);
    }

    /**
     * 使用手机号注册账号
     * @author 骆超
     * @date 2021/7/14
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("registerAccount")
    public Result registerAccount(@RequestBody SysUser sysUser){
        return sysUserService.registerAccount(sysUser);
    }


    /**
     * 发送找回密码的短信
     * @author 骆超
     * @date 2021/7/14
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("sendBackCode")
    public Result sendBackCode(@RequestBody SysUser sysUser){
        return sysUserService.sendBackCode(sysUser);
    }


    /**
     * 找回密码
     * @author 骆超
     * @date 2021/7/14
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("backPassword")
    public Result backPassword(@RequestBody SysUser sysUser){
        return sysUserService.backPassword(sysUser);
    }

    /**
     * 发送手机换绑短信
     * @author 骆超
     * @date 2021/7/16
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("sendReplaceMsg")
    public Result sendReplaceMsg(@RequestBody SysUser sysUser){
        return sysUserService.sendReplaceMsg(sysUser);
    }

    /**
     * 手机号换绑
     * @author 骆超
     * @date 2021/7/16
     * @param sysUser 参数说明
     * @return com.lc.activiti.config.Result
     */
    @PostMapping("replacePhoneNum")
    public Result replacePhoneNum(@RequestBody SysUser sysUser){
        return sysUserService.replacePhoneNum(sysUser);
    }

}
